package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.LoaiDanhMucFullDao;
import com.tt.fashionWebSite.dao.LoaiDanhMucSanPhamDao;
import com.tt.fashionWebSite.domain.LoaiDanhMucFull;
import com.tt.fashionWebSite.domain.LoaiDanhMucSanPham;
import com.tt.fashionWebSite.dto.LoaiDanhMucFullDto;
import com.tt.fashionWebSite.dto.LoaiDanhMucSanPhamFullDto;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.service.WomenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by isoft-work 5 on 12/6/2014.
 */
@Service("womenService")
public class WomenServiceImp extends GenericService implements WomenService {
   @Autowired
   @Qualifier("loaiDanhMucFull")
   private LoaiDanhMucFullDao loaiDanhMucFullDao;

   @Autowired
   @Qualifier("loaiDanhMucSanPham")
   private LoaiDanhMucSanPhamDao loaiDanhMucSanPhamDao;

   @Override
   public List<LoaiDanhMucFullDto> getListMenu(String gender) {
      List<LoaiDanhMucFull> loaiDanhMucFull = loaiDanhMucFullDao.getListMenu();
//      check gender for menu
      if(!gender.equals("NAM")) {
         gender = "NỮ";
      }
      for(int i = 0; i < loaiDanhMucFull.size(); i++) {
         List<LoaiDanhMucSanPham> loaiDanhMucSanPhams = loaiDanhMucFull.get(i).getLoaiDanhMucSanPham();
         for(int j = 0; j < loaiDanhMucSanPhams.size(); j++) {
            String phanLoai = loaiDanhMucSanPhams.get(j).getPhanLoai();
            if(phanLoai == null || gender.equals(phanLoai)) {

            }
            else {
               loaiDanhMucSanPhams.remove(j);
               j -= 1;
            }
         }
      }

      List<LoaiDanhMucFullDto> loaiDanhMucFullDto = mapper.mapCollection(loaiDanhMucFull, LoaiDanhMucFullDto.class, LOAI_DANH_MUC_FULL_MAP_ID);
      return loaiDanhMucFullDto;
   }

   @Override
   public LoaiDanhMucFullDto getListMenuByIdDM(String idDMSP, String gender) {
      LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(idDMSP);
      LoaiDanhMucFull loaiDanhMucFull = loaiDanhMucFullDao.findById(loaiDanhMucSanPham.getLoaiDanhMuc().getMaLoai());
      LoaiDanhMucFullDto loaiDanhMucFullDto = mapper.map(loaiDanhMucFull, LoaiDanhMucFullDto.class, LOAI_DANH_MUC_FULL_MAP_ID);
      List<LoaiDanhMucSanPhamFullDto> loaiDanhMucSanPhams = loaiDanhMucFullDto.getLoaiDanhMucSanPham();

      if(!gender.equals("NAM")) {
         gender = "NỮ";
      }
      if(loaiDanhMucSanPhams != null && loaiDanhMucSanPhams.size() > 0) {
         for(int i = 0; i < loaiDanhMucSanPhams.size(); i++) {
            String loai = loaiDanhMucSanPhams.get(i).getPhanLoai();
            if(loai == null || loai.equals(gender)) {

            }
            else {
               loaiDanhMucSanPhams.remove(i);
               i -= 1;
            }
         }
      }
      return loaiDanhMucFullDto;
   }
}
